﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Intergr8.Data;
using Intergr8.IO.File;

namespace Intergr8.Data.CSVDataSource
{
    public class CSVDataProvider : DataProvider
    {
        /// <summary>
        /// Path to the folder containing the data
        /// </summary>
        private string _folderpath = "";

        /// <summary>
        /// Filename containing the data to read
        /// </summary>
        private string _filename = "";

        #region Initialise
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            try
            {
                System.Diagnostics.Trace.WriteLine("Initialising CSVDataProvider");

                // Initialise the base first
                base.Initialize(name, config);

                // Pull out any of our custom attributes
                if (config["Folder"] != null)
                {
                    _folderpath = config["Folder"];
                }
                else
                {
                    throw new DataProviderException("CSVDataProvider", "The SQLDataProvider needs a valid 'Folder' property to find the given filename.");
                }

                if (config["Filename"] != null)
                {
                    _filename = config["Filename"];
                }
                else
                {
                    throw new DataProviderException("CSVDataProvider", "The CSVDataProvider needs a 'Filename' property to identify the file to read.");
                }

                if (_folderpath == "")
                {
                    throw new DataProviderException("CSVDataProvider", "The CSVDataProvider needs a 'Folder' value to identify the repository.");
                }
                if (_filename == "")
                {
                    throw new DataProviderException("CSVDataProvider", "The CSVDataProvider needs a 'Filename' value to open the file.");
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("CSVDataProvider Exception " + ex.Message);
                throw ex;
            }
            finally
            {
            }
        }
        #endregion

        public override System.Data.DataTable Fetch()
        {
            try
            {

                System.Collections.Generic.List<System.Type> colTypes = new List<Type>();
                colTypes.Add(typeof(string));
                colTypes.Add( typeof(int));
                colTypes.Add(typeof(float));

                DataSetIO dsio = new DataSetIO(_folderpath, _filename, colTypes);

                DataSet data = dsio.Read();

                DataTable dt = data.Tables[DataSetIO.DATATABLENAME];

                //foreach (DataRow dr in dt.Rows)
                //{
                //    System.Console.WriteLine(String.Format("{0} {1} {2} {3} {4} {5}", dr[0], dr[1], dr[2], dr[3], dr[4], dr[5]));
                //}

                return dt;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.Message);
                throw new DataProviderException("CSVDataProvider", "Error generating a DataTable", ex);
            }

        }
    }
}
